package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Share;
import com.ruoyi.system.mapper.ShareMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MemberMapper;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.service.IMemberService;
import com.ruoyi.common.core.text.Convert;

/**
 * 业务员Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-10-16
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ShareMapper shareMapper;

    /**
     * 查询业务员
     * 
     * @param id 业务员ID
     * @return 业务员
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询业务员列表
     * 
     * @param member 业务员
     * @return 业务员
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增业务员
     * 
     * @param member 业务员
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改业务员
     * 
     * @param member 业务员
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 删除业务员对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String ids)
    {
        return memberMapper.deleteMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除业务员信息
     * 
     * @param id 业务员ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }

    /**
     * 查询业务员树列表
     * 
     * @return 所有业务员信息
     */
    @Override
    public List<Ztree> selectMemberTree()
    {
        List<Member> memberList = memberMapper.selectMemberList(new Member());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Member member : memberList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(member.getId());
            ztree.setpId(member.getPid());
            ztree.setName(member.getNickName());
            ztree.setTitle(member.getNickName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 查询业务员树列表-客户分享
     *
     * @return 业务员信息-客户分享
     */
    @Override
    public List<Ztree> selectMemberTreeShare(Long customerId)
    {
        List<Member> memberList = memberMapper.selectMemberList(new Member());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Member member : memberList)
        {
            Share share = new Share();
            share.setCustomerId(customerId);
            share.setMemberId(member.getId());
            List<Share> shareList = shareMapper.selectShareList(share);
            if(shareList.size()>0){
                continue;
            }
            Ztree ztree = new Ztree();
            ztree.setId(member.getId());
            ztree.setpId(member.getPid());
            ztree.setName(member.getNickName());
            ztree.setTitle(member.getNickName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
